<template>
	<cu-custom :isBack="true">
		<template #title>整改材料审核</template>
	</cu-custom>
	
	<view class="theme-shadow mb-2">
		<view class="report-row" v-for="(item, index) in dataList" :key="index" @click="proviewDoc(item)">
			<text class="text-ellipsis-1 mid-ellipsis">{{ item.title }}</text>
			<text class="cuIcon-right text-gray"></text>
		</view>
	</view>
	<view v-if="dataList.length === 0" class="empty-hint">
		<image src="../../static/images/empty-message.png" mode="aspectFit"></image>
		<view class="text-gray small-text">这条整改记录没有上传任何材料</view>
	</view>
	<view v-else class="text-gray text-center">
		没有更多了
	</view>
	
	<view class="handle-bar">
		<textarea
			:enable-native="false"
			placeholder="请填写审合意见"
			maxlength="500"
			cols="30"
			rows="10"
			style="height:200rpx"
			v-model="textarea"
		></textarea>
		<view class="button-bar">
			<view style="width: 48%;"><CuButton plain @click="handleIgnore">不通过</CuButton></view>
			<view style="width: 48%;"><CuButton @click="handleAccept">通过</CuButton></view>
		</view>	
	</view>
</template>

<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app"
import { ref, type Ref } from 'vue';
import { rectAudit } from "@/api/mission.js"
import { getSuffix } from "@/utils/base"
import { router } from "@/router" 
import store from "@/store/index.js"
import CuButton from "@/components/CuButton.vue"

interface IReport {
	id: number
	title: string
	filepath: string
	suffix: string 
}
interface IFile {
	name: string
	url: string
}

const props = defineProps({
	mission_id: {
		type: String,
		default: null
	}
})

let rect_id: string
const dataList:Ref<IReport[]> = ref([])
const textarea: Ref<string> = ref("")

const proviewDoc = (e: IReport) => {
	if (e.filepath) {
		if (e.suffix === "jpg" || e.suffix === "png" || e.suffix === "jpeg") {
			uni.previewImage({
				urls: [e.filepath],
				current: e.filepath
			})
		} else if (e.suffix === "docx") {
			if (store.getters.osName === 'ios') {
				router.push({
					path: "/pages/index/word",
					params: {
						title: e.title,
						filepath: e.filepath
					}
				})
			} else {
				location.href = e.filepath
			}
		} else {
			uni.showToast({
				icon: "none",
				title: "当前文件格式不支持在线浏览"
			})
		}
	} else {
		uni.showToast({
			icon: "none",
			title: "报告生成中 请稍后查看"
		})
	}
}

const handleIgnore = () => {
	uni.showLoading()
	rectAudit({
		id: rect_id,
		opinions: textarea.value,
		isPass: false
	}).then((res) => {
		uni.hideLoading()
		uni.showToast({
			icon: "none",
			title: "操作成功"
		})
		setTimeout(() => {
			router.back()
		}, 1000)
	})
}
const handleAccept = () => {
	uni.showLoading()
	rectAudit({
		id: rect_id,
		opinions: textarea.value,
		isPass: true
	}).then((res) => {
		uni.hideLoading()
		uni.showToast({
			icon: "none",
			title: "操作成功"
		})
		setTimeout(() => {
			router.back()
		}, 1000)
	})
}

const loadData = () => {
	console.log("tempRect", store.getters.tempRect)
	rect_id = store.getters.tempRect.rectificationTaskId
	console.log("rectificationTaskId", rect_id)
	dataList.value = Array.from(store.getters.tempRect.files, (file: any) => ({
		id: 0,
		title: file.fileName,
		filepath: file.fileUrl,
		suffix: getSuffix(file.fileUrl)
	}))
}

onLoad((options: any) => {
	loadData()
})

</script>

<style lang="stylus" scoped>
body
	background-color #fff
.report-row
	display: flex
	align-items: center
	padding: 30rpx 40rpx
	justify-content: left
	border-bottom: 2rpx solid #eeeeee
	background-color #ffffff
	position: relative;
	overflow: hidden
	width: 750rpx
	font-size 34rpx
	.id-row
		color: #2a2a2a
		white-space: nowrap
		width: 180rpx
	.mid-ellipsis
		width: 650rpx
.report-row:active
	background-color #efefef
	
.empty-hint
	text-align: center
	
.handle-bar
	position: fixed
	bottom: 0
	width: 100%
	padding: 40rpx 10%
	// margin: 0 10% 40rpx
	background-color: #fff
	
	.button-bar
		display: flex
		justify-content: space-between
</style>
